﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using Lapas.Common;
using WorkDAL.Users;
using Common;

namespace Users
{
    public partial class UsersListForm : ListForm
    {
        public UsersListForm()
        {
            InitializeComponent();
            UIFormat.FormatGrid(mainGrid, false, true, true, false);
        }

        protected override DataGridView Grid
        {
            get
            {
                return this.mainGrid;
            }
        }

        protected override void FormatGrid()
        {
            UIFormat.FormatGrid(mainGrid, false, true, true, false);
            UIFormat.SetGridColumnFormat(ColUserName, FormatType.NormalText);
            UIFormat.SetGridColumnFormat(ColUserFullName, FormatType.NormalText);
        }

        private void UsersListForm_Load(object sender, EventArgs e)
        {
            GetData();
        }

        private void GetData()
        {
            try
            {
                List<tbUser> userList = new List<tbUser>();

                if (CommonSettings.isSuperAdmin)
                    userList = UserDataAccess.Instance().UserGetList();
                else
                    userList = UserDataAccess.Instance().UserGetList(CommonSettings.UserBranchCode);

                bindingSource.DataSource = userList;
                mainGrid.DataSource = bindingSource;
                BindData();
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
                return;
            }
        }

        protected override void BindData()
        {
            base.BindData();
        }

        protected override void DisplayEditForm(EditForm.EditFormAction action)
        {
            UsersEditForm usersEditForm = new UsersEditForm();
            usersEditForm.FormAction = action;

            if (action == EditForm.EditFormAction.Change)
            {
                usersEditForm.user = (tbUser)bindingSource.Current;
            }

            usersEditForm.ShowDialog();
            if (usersEditForm.Reload)
            {
                GetData();
            }
        }

        private void tsbChangePass_Click(object sender, EventArgs e)
        {
            UsersChangePassWordForm changePassForm = new UsersChangePassWordForm();
            changePassForm.users = (tbUser)bindingSource.Current;

            changePassForm.ShowDialog();
            if (changePassForm.Reload)
            {
                GetData();
            }
        }

        private void stbPermission_Click(object sender, EventArgs e)
        {
            UsersPermissionForm permissionForm = new UsersPermissionForm();
            permissionForm.userPermission = UserDataAccess.Instance().GetMenuPermission(((tbUser)bindingSource.Current).UserName);
            permissionForm.user = (tbUser)bindingSource.Current;

            permissionForm.ShowDialog();
            if (permissionForm.Reload)
            {
                GetData();
            }
        }
    }
}
